<?php $url = new Url();?>
<script src="<?=Yii::app()->params["urlRs"] ?>/js/tiny_mce/tiny_mce.js" type="text/javascript"></script>     
<div id="main" class="nosidebar">    
    <div id="content">

        <div class="flash notice" id="flash_notice" style="display: none;">Thêm mới tin tức thành công!</div>

        <div id="errorExplanation" style="display: none;">
            <ul id="err_msg">
                <!--<li>Tên không thể để trắng</li>
                <li>Mã nhận dạng không thể để trắng</li>
                <li>Mã nhận dạng quá ngắn (tối thiểu 1 ký tự)</li>-->
            </ul>
        </div>
    
        <div style="" id="add-news">
            <h2>Thêm tin</h2>
            <form method="post" id="news-form" enctype="multipart/form-data" class="new_news" action="/projects/tes2342/news" accept-charset="UTF-8">            
                <div class="box tabular">
                    <p><label for="news_title">Tiêu đề<span class="required"> *</span></label><input type="text" value="" size="60" name="news[title]" id="news_title"></p>
                    <p><label for="news_summary">Tóm tắt</label><textarea rows="2" name="news[summary]" id="new_description" cols="60"></textarea></p>
                    <p><label for="news_description">Nội dung<span class="required"> *</span></label>
                        <textarea cols="12" rows="12" id="new_content"></textarea>                        
                    </p>                    
                </div>            
                <input type="button" value="Tạo" name="commit" id="submit_button" onclick="add_new()">
            </form>
        </div>

        <div style="clear:both;"></div>
    </div>
</div>

<script type="">

    tinyMCE.init({
        theme : "advanced",
        mode : "exact",
        elements : "new_content",
        width : "800px",
        theme_advanced_buttons1 : "mylistbox,mysplitbutton,bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,undo,redo,link,unlink",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom"
    });
    
    function add_new(){
        tinyMCE.triggerSave();
        
        var news_title = $('#news_title').val();       
        var new_description = $('#new_description').val();       
        var new_content = $('#new_content').val();               

        var flag_err = 0;
        var err_msg = '';
        $('#errorExplanation').slideUp('slow');
        $('#err_msg').empty();
        if(news_title == ''){
            flag_err = 1;
            err_msg = '<li>Chưa điền tiêu đề tin tức!</li>'
            $('#err_msg').append(err_msg);
        }
        if(new_content == ''){
            flag_err = 1;
            err_msg = '<li>Chưa nhập nội dung tin tức!</li>'
            $('#err_msg').append(err_msg);
        }        
        if(flag_err == 1){
            $('#errorExplanation').slideDown('slow');
            return false;    
        }else{            
            $('#errorExplanation').slideUp('slow');
            var strUrl = '<?=$url->createUrl("new/ajaxAddNew") ?>';            
            loadingAjax();
            $.ajax({
                type: "POST",
                url: strUrl,
                data: {news_title:news_title,new_description:new_description,new_content:new_content},
                success: function(msg){                            
                    if(msg==1){                       
                        $('#errorExplanation').slideUp('slow');
                        $('#flash_notice').slideDown('slow');
                    }
                    $("#submit_button").removeAttr("disabled");

                },
                beforeSend:function(){
                    $("#submit_button").attr("disabled","disabled");                
                }
            });
        }  
        closeLoadingAjax();
    }

</script>
